package com.ticket.sass.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ticket.sass.common.entity.tenant.TenantAccountEntity;
import com.ticket.sass.provider.mapper.TenantAccountMapper;
import com.ticket.sass.provider.service.BaseService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ywb
 * @since 2024-03-19
 */
@Service
public class TenantAccountServiceImpl extends ServiceImpl<TenantAccountMapper, TenantAccountEntity> implements BaseService<TenantAccountEntity> {
    public BigDecimal getBalance() {
        QueryWrapper<TenantAccountEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("balance");
        TenantAccountEntity entity = this.baseMapper.selectOne(queryWrapper);
        return entity != null ? entity.getBalance() : null;
    }
    
    public BigDecimal getBalance(Long tenantId) {
        QueryWrapper<TenantAccountEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("balance");
        queryWrapper.eq("tenant_id", tenantId);
        TenantAccountEntity entity = this.baseMapper.selectOne(queryWrapper);
        return entity != null ? entity.getBalance() : null;
    }
    
    
    public Map<Long, BigDecimal> getBalancesByTenantIds(List<Long> tenantIds) {
        Map<Long, BigDecimal> balancesMap = new HashMap<>();
        
        // 使用 QueryWrapper 构建查询条件
        QueryWrapper<TenantAccountEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("tenant_id", tenantIds);
        
        // 查询符合条件的租户账户列表
        List<TenantAccountEntity> tenantAccounts = this.baseMapper.selectList(queryWrapper);
        
        // 将查询结果中的租户ID和账户余额放入 Map 中
        for (TenantAccountEntity tenantAccount : tenantAccounts) {
            balancesMap.put(tenantAccount.getTenantId(), tenantAccount.getBalance());
        }
        
        return balancesMap;
    }
}
